package test;

import core.Building;
import core.Colony;
import core.Member;
import core.Settings;
import core.Events;
import core.Events.Types;
import groovy.util.GroovyTestCase;

class ColonyTest extends GroovyTestCase {

	public void testNewColony() {
		Colony col = new Colony("C1")
		assert col.name == "C1"
		assert col.members == []
		assert col.buildings == []

		Member didier = new Member('Gultar Ze Terrible')
		assert didier.name == "Gultar Ze Terrible"
		assert didier.health == Settings.initialMemberHealth
		assert didier.isSick == false

		Member sylvie = new Member('Cinderella')
		assert sylvie.name == "Cinderella"
		assert sylvie.health == Settings.initialMemberHealth

		col.members += [didier, sylvie]
		assert col.members.size() == 2
		assert col.members == [didier, sylvie]
		assert col.members.class == java.util.ArrayList.class

		def storage1 = new Building('STORE')
		assert storage1.type == 'STORE'
		assert storage1.health == Settings.initialBuildingHealth

		def farm1 = new Building('FARM')
		assert farm1.type == 'FARM'

		def med1 = new Building('MEDICAL_UNIT')
		assert med1.type == 'MEDICAL_UNIT'

		def fact1 = new Building('FACTORY')
		assert  fact1.type == 'FACTORY'

		def solarplant1 = new Building('SOLAR_PLANT')
		assert solarplant1.type == 'SOLAR_PLANT'

		def mine1 = new Building('MINE')
		assert mine1.type == 'MINE'

		try { 
			new Building('NIMPORTNAOUAK')
			assert false, 'should have failed' 
			}
		catch( Exception e ) { /* ok */ }
		
		col.buildings += [
			storage1,
			farm1,
			med1,
			fact1,
			solarplant1,
			mine1
		]
		assert col.buildings == [
			storage1,
			farm1,
			med1,
			fact1,
			solarplant1,
			mine1
		]

		assert col.food == Settings.initialFood
		assert col.energy == Settings.initialEnergy
		assert col.materials == Settings.initialMaterials
		col.produce()
		assert col.food == Settings.initialFood + Settings.foodProdRate
		assert col.energy == Settings.initialEnergy + Settings.energyProdRate
		assert col.materials == Settings.initialMaterials + Settings.materialProdRate
	}

	public void testRandomEvents() {
		assert Settings.randomSeed == 1 // pre-computed results for random seed = 1
		Colony col = new Colony("Outerspace XXX")
		def stats = [
			(Events.Types.CONTAGIOUS_DISEASE):0,
			(Events.Types.THUNDER_STRIKE):0,
			(Events.Types.CROP_PEST):0
		]

		def tot = 0
		(1..100).each { n ->
			List l = col.randomEvents()
			if (l != null) {
				l.each { eventType ->
					stats[eventType]++
				}
			}
		}

		assert stats[(Events.Types.CONTAGIOUS_DISEASE)] == 9
		assert stats[(Events.Types.THUNDER_STRIKE)] == 5
		assert stats[(Events.Types.CROP_PEST)] == 5

	}

	public void testProcessEvents() {
		assert Settings.randomSeed == 1
		Colony col = new Colony("Outerspace XXX")

		col.members = [
			new Member('Arthur Martin'),
			new Member('Bahia Benmaklouf'),
			new Member('Ford Prefect')
		]
		assert col.members.grep({it.isSick}) == []

		List events = [
			(Events.Types.CONTAGIOUS_DISEASE)
		]
		col.processEvents(events)
		assert col.members.grep({it.isSick}) != []

		Building farm = new Building( 'FARM' )
		col.buildings = [farm]
		assert farm.health == Settings.initialBuildingHealth
		events = [
			(Events.Types.THUNDER_STRIKE)
		]
		col.processEvents(events)
		assert farm.health < Settings.initialBuildingHealth

		col.food = 400
		events = [(Events.Types.CROP_PEST)]
		col.processEvents(events)
		assert col.food < 400

	}

	public void testToXML() {
		Colony col = new Colony("Outerspace XXX")

		col.members = [
			new Member('Arthur Martin'),
			new Member('Bahia Benmaklouf'),
			new Member('Ford Prefect')
			]
		
		col.buildings = [
			new Building('FARM'),
			new Building('MINE'),
			new Building('STORE')
			]

		col.food = 1000
		col.structure = 100
		col.energy = 1000
		col.materials = 500
		assert col.toXML() ==
'''<colony name='Outerspace XXX' food='1000' structure='100' energy='1000' materials='500'>
  <members>
    <member name='Arthur Martin' health='100' />
    <member name='Bahia Benmaklouf' health='100' />
    <member name='Ford Prefect' health='100' />
  </members>
  <buildings>
    <building type='FARM' health='100' />
    <building type='MINE' health='100' />
    <building type='STORE' health='100' />
  </buildings>
</colony>'''

		def xml = col.toXML() 	// old values
		col.name = 'zloubi'		// new values...
		col.food = 2000
		col.structure = 200
		col.energy = 2000
		col.materials = 5000
		col.members = [ new Member('bozo')]
		col.fromXML(xml)
		assert col.name == 'Outerspace XXX'
		assert col.food == 1000
		assert col.structure == 100
		assert col.energy == 1000
		assert col.materials == 500
		assert col.members.size() == 3

	}

}